package com.ecoolex.chinadutyfree.retailer.admin.graphql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.admin.core.AdminConstant;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsAfterService;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsAfterService.Column;
import com.ecoolex.chinadutyfree.retailer.pojo.AddGoodsAfterServiceDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.EditGoodsAfterServiceDTO;
import com.ecoolex.chinadutyfree.retailer.ws.GoodsService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.google.common.collect.Lists;


@GraphQL2Module(type = Type.Admin, description = "商品售后服务模板管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY,
	key = GoodsAfterServiceGraphql.AUTH,
	name = "商品售后服务模板管理")
public class GoodsAfterServiceGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-GOODS-AFTER-SERVICE";

	@Autowired
	private GoodsService service;

	private static final String AUTH_GET = AUTH + "-GET";

	@QueryField(field = "goodsAfterService", description = "获取商品售后服务详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取商品售后服务详情")
	public GoodsAfterService get(@NonNull Integer afterServiceId, AdminMemberAuthority user) {
		user.assertLogin();
		return service.getAfterServiceById(afterServiceId);
	}

	private static final String AUTH_QUERY = AUTH + "-QUERY";

	@QueryField(field = "goodsAfterServiceList", description = "查询商品售后服务列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询商品售后服务列表")
	public Pagination<GoodsAfterService> query(String name, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
		user.assertLogin();
		return service.queryAfterService(name, sorts, page);
	}

	private static final String AUTH_ADD = AUTH + "-ADD";

	@MutationField(field = "addGoodsAfterService", description = "添加商品售后服务")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加商品售后服务")
	public GoodsAfterService add(@NonNull AddGoodsAfterServiceDTO add, AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return service.addAfterService(add, user.userName());
	}

	private static final String AUTH_EDIT = AUTH + "-EDIT";

	@MutationField(field = "editGoodsAfterService", description = "编辑商品售后服务")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑商品售后服务")
	public GoodsAfterService edit(@NonNull EditGoodsAfterServiceDTO edit, AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		GoodsAfterService record = service.getAfterServiceById(edit.getAfterServiceId());
		Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "商品售后服务不存在");
		BeanUtil.copy(edit,
			record,
			Lists.newArrayList(Column.deleted, Column.createUser, Column.createTime).stream().map(
				Column::getJavaProperty).toArray(String[]::new));
		return service.editAfterService(record, user.userName());
	}

	private static final String AUTH_DEL = AUTH + "-DEL";

	@MutationField(field = "delGoodsAfterService", description = "删除商品售后服务")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除商品售后服务")
	public boolean del(@NonNull Integer afterServiceId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		GoodsAfterService record = service.getAfterServiceById(afterServiceId);
		Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "商品售后服务不存在");
		service.deleteAfterService(record, user.userName());
		return true;
	}
}

